home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d939.lha / ExtraCmds / source_etc.lha / src / Prepare.c < prev    next >
C/C++ Source or Header  |  1993-10-22  |  6KB  |  234 lines

  1. /*   ---------------------------------      -------     
  2.  *   |\  | | | | |  |.| |   \|  |/ /|\      |||||||     
  3.  *   | | | |/  | |\ |/  |/|  |\ |/  |    ?  ---+---  =< 
  4.  *   | | | |   | |  |     |  |  |   |     \qqqqqqqqq/   
  5.  *   ---------------------------------  ~~~~~~~~~~~~~~~~
  6.  *  Prepare - Splits text files into words
  7.  *  Copyright (C) 1993 Torsten Poulin
  8.  *
  9.  *  This program is free software; you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation; either version 2 of the License, or
  12.  *  (at your option) any later version.
  13.  *
  14.  *  This program is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *  GNU General Public License for more details.
  18.  *
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with this program; if not, write to the Free Software
  21.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  *
  23.  *  The author can be contacted by s-mail at
  24.  *    Torsten Poulin
  25.  *    Banebrinken 99, 2, 77
  26.  *    DK-2400 Copenhagen NV
  27.  *    DENMARK
  28.  *
  29.  * $Id: Prepare.c,v 37.4 93/03/01 12:55:03 Torsten Rel $
  30.  * $Log:    Prepare.c,v $
  31.  * Revision 37.4  93/03/01  12:55:03  Torsten
  32.  * Changed all occurrences of "struct DosBase *" to "struct DosLibrary *"
  33.  * 
  34.  * Revision 37.3  93/02/24  16:56:39  Torsten
  35.  * Added HYPHEN switch. When given hyphenated compounds will be
  36.  * considered one word.
  37.  * 
  38.  * Revision 37.2  93/02/24  11:56:44  Torsten
  39.  * Can fold characters to lower or upper case.
  40.  * 
  41.  * Revision 37.1  93/02/24  00:27:26  Torsten
  42.  * initial revision.
  43.  * 
  44.  */
  45.  
  46. #include <exec/types.h>
  47. #include <exec/memory.h>
  48. #include <dos/dos.h>
  49. #include <dos/dosasl.h>
  50. #include <clib/dos_protos.h>
  51. #include <clib/exec_protos.h>
  52. #ifdef __SASC
  53. #include <pragmas/dos_pragmas.h>
  54. #include <pragmas/exec_pragmas.h>
  55. #endif
  56. #include "tastlib.h"
  57. #include "prepare_rev.h"
  58.  
  59. #define PROGNAME "Prepare"
  60. #define TEMPLATE "FROM/M,TO/K,TOLOWER/S,TOUPPER/S,HYPHEN/S"
  61. #define OPT_FROM  0
  62. #define OPT_TO    1
  63. #define OPT_LOWER 2
  64. #define OPT_UPPER 3
  65. #define OPT_HYPH  4
  66.  
  67. char const versionID[] = VERSTAG;
  68. char const copyright[] = "$COPYRIGHT:Copyright © 1993 Torsten Poulin$";
  69.  
  70. typedef struct {
  71.   struct DosLibrary *DOSBase;
  72.   BOOL tolower;
  73.   BOOL toupper;
  74.   BOOL hyphen;
  75.   BPTR output;
  76. } Global;
  77.  
  78. LONG prepare(UBYTE *filename, Global *global);
  79.  
  80.  
  81. LONG entrypoint(VOID)
  82. {
  83.   struct DosLibrary *DOSBase;
  84.   struct RDArgs *args;
  85.   Global *global;
  86.   LONG arg[5];
  87.   LONG rc = RETURN_OK;
  88.  
  89.   if (!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  90.     return RETURN_FAIL;
  91.  
  92.   if (!(global = AllocVec(sizeof(Global), MEMF_PUBLIC | MEMF_CLEAR)))
  93.   {
  94.     PrintFault(ERROR_NO_FREE_STORE, PROGNAME);
  95.     rc = RETURN_FAIL;
  96.   }
  97.   else
  98.   {
  99.     global->DOSBase = DOSBase;
  100.  
  101.     arg[OPT_FROM] = arg[OPT_TO] =
  102.       arg[OPT_LOWER] = arg[OPT_UPPER] = arg[OPT_HYPH] = 0L;
  103.     
  104.     if (!(args = ReadArgs(TEMPLATE, arg, NULL)))
  105.     {
  106.       printerror(PROGNAME, global);
  107.       rc = RETURN_ERROR;
  108.     }
  109.     else
  110.     {
  111.       global->hyphen  = (BOOL) arg[OPT_HYPH];
  112.       global->tolower = (BOOL) arg[OPT_LOWER];
  113.       global->toupper = (BOOL) arg[OPT_UPPER];
  114.       if (global->tolower && global->toupper)
  115.       {
  116.     PutStr("TOLOWER and TOUPPER are mutually exclusive\n");
  117.     rc = RETURN_ERROR;
  118.       }
  119.       else
  120.       {
  121.     if (!arg[OPT_TO])
  122.       global->output = Output();
  123.     else if (!(global->output=Open((UBYTE *)arg[OPT_TO], MODE_NEWFILE)))
  124.     {
  125.       PutStr("Cannot open ");
  126.       PutStr((UBYTE *) arg[OPT_TO]);
  127.       PutStr("\n");
  128.       rc = RETURN_ERROR;
  129.     }
  130.  
  131.     if (global->output)
  132.       if (!arg[OPT_FROM])
  133.         rc = prepare(NULL, global);
  134.       else
  135.         rc = foreach((UBYTE **) arg[OPT_FROM], prepare, global);
  136.     if (arg[OPT_TO])
  137.       Close(global->output);
  138.       }
  139.       FreeArgs(args);
  140.  
  141.       if (rc == ERROR_BREAK)
  142.       {
  143.     PrintFault(ERROR_BREAK, NULL);
  144.     rc = RETURN_WARN;
  145.       }
  146.       else if (rc == ERROR_NO_FREE_STORE)
  147.       {
  148.     PrintFault(ERROR_NO_FREE_STORE, PROGNAME);
  149.     rc = RETURN_FAIL;
  150.       }
  151.       else if (rc != RETURN_OK)
  152.     printerror(PROGNAME, global);
  153.     }
  154.     FreeVec(global);
  155.   }
  156.   CloseLibrary((struct Library *) DOSBase);
  157.   return rc;
  158. }
  159.  
  160.  
  161. /*
  162.  * Valid only for the ISO Latin 1 character set.
  163.  */
  164. BOOL isletter(LONG c)
  165. {
  166.   return ((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>=0xC0 && c<=0xFF))
  167.     && c != 0xD7 && c != 0xF7;
  168. }
  169.  
  170.  
  171. LONG prepare(UBYTE *filename, Global *global)
  172. {
  173.   struct DosLibrary *DOSBase = global->DOSBase;
  174.   register UBYTE breakcheck = 0;
  175.   LONG c;
  176.   BOOL inword = FALSE;
  177.   LONG rc = RETURN_OK;
  178.   BPTR input = Input();
  179.     
  180.   if (filename)
  181.     if (!(input = Open(filename, MODE_OLDFILE)))
  182.     {
  183.       PutStr("Cannot open ");
  184.       printerror(filename, global);
  185.       return RETURN_ERROR;
  186.     }
  187.  
  188.   while ((c = FGetC(input)) != EOF)
  189.   {
  190.     if (isletter(c))
  191.     {
  192.       inword = TRUE;
  193.       if (global->tolower && c != 0xDF)
  194.     c |= 0x20;
  195.       else if (global->toupper && c!= 0xFF)
  196.     c &= ~0x20;
  197.       FPutC(global->output, c);
  198.     }
  199.     else if (inword)
  200.     {
  201.       if (c == '\'')
  202.       {
  203.     /* Don't strip an in-word apostrophe */
  204.     c = FGetC(input);
  205.     if (isletter(c))
  206.       FPutC(global->output, '\'');
  207.     UnGetC(input, c);
  208.       }
  209.       else if (global->hyphen && c == '-')
  210.       {
  211.     /* Don't strip a hyphen */
  212.     c = FGetC(input);
  213.     if (isletter(c))
  214.       FPutC(global->output, '-');
  215.     UnGetC(input, c);
  216.       }
  217.       else
  218.       {
  219.     inword = FALSE;
  220.     FPutC(global->output, '\n');
  221.       }
  222.     }
  223.  
  224.     if (!(breakcheck -= 4) && CheckSignal(SIGBREAKF_CTRL_C))
  225.     {
  226.       rc = ERROR_BREAK;
  227.       break;
  228.     }
  229.   }
  230.   if (filename)
  231.     Close(input);
  232.   return rc;
  233. }
  234.